package edu.biwu.filter02;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebInitParam;

import java.io.IOException;

/**
 *
 */
@WebFilter(filterName = "charsetFilter",urlPatterns = "/*",initParams = {
        @WebInitParam(name = "charset",value = "UTF-8")
})
public class CharsetFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletContext servletContext = servletRequest.getServletContext();
        FilterRegistration charsetFilter = servletContext.getFilterRegistration("charsetFilter");

        servletResponse.setContentType("text/html;charset = " + charsetFilter.getInitParameter("charset"));
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
